package org.cybzacg.encryption.model;

import org.cybzacg.encryption.enums.EncryptionAlgorithm;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.List;
import java.util.Objects;

/**
 * 注册统计信息类
 * 封装加密器注册的统计数据
 * 
 * @author encryption team
 * @version 1.0
 */
public class RegistrationStatistics {
    
    private final long totalRegistrations;
    private final long successfulRegistrations;
    private final long failedRegistrations;
    private final Map<EncryptionAlgorithm, Integer> algorithmCounts;
    private final List<String> registeredClassNames;
    private final LocalDateTime timestamp;
    
    public RegistrationStatistics(long totalRegistrations, 
                                long successfulRegistrations, 
                                long failedRegistrations,
                                Map<EncryptionAlgorithm, Integer> algorithmCounts,
                                List<String> registeredClassNames) {
        this.totalRegistrations = totalRegistrations;
        this.successfulRegistrations = successfulRegistrations;
        this.failedRegistrations = failedRegistrations;
        this.algorithmCounts = algorithmCounts;
        this.registeredClassNames = registeredClassNames;
        this.timestamp = LocalDateTime.now();
    }
    
    /**
     * 获取总注册次数
     * 
     * @return 总注册次数
     */
    public long getTotalRegistrations() {
        return totalRegistrations;
    }
    
    /**
     * 获取成功注册次数
     * 
     * @return 成功注册次数
     */
    public long getSuccessfulRegistrations() {
        return successfulRegistrations;
    }
    
    /**
     * 获取失败注册次数
     * 
     * @return 失败注册次数
     */
    public long getFailedRegistrations() {
        return failedRegistrations;
    }
    
    /**
     * 获取算法计数映射
     * 
     * @return 算法计数映射
     */
    public Map<EncryptionAlgorithm, Integer> getAlgorithmCounts() {
        return algorithmCounts;
    }
    
    /**
     * 获取已注册的类名列表
     * 
     * @return 已注册的类名列表
     */
    public List<String> getRegisteredClassNames() {
        return registeredClassNames;
    }
    
    /**
     * 获取时间戳
     * 
     * @return 时间戳
     */
    public LocalDateTime getTimestamp() {
        return timestamp;
    }
    
    /**
     * 获取成功率
     * 
     * @return 成功率（0.0-1.0）
     */
    public double getSuccessRate() {
        if (totalRegistrations == 0) {
            return 0.0;
        }
        return (double) successfulRegistrations / totalRegistrations;
    }
    
    /**
     * 获取失败率
     * 
     * @return 失败率（0.0-1.0）
     */
    public double getFailureRate() {
        if (totalRegistrations == 0) {
            return 0.0;
        }
        return (double) failedRegistrations / totalRegistrations;
    }
    
    /**
     * 获取支持的算法数量
     * 
     * @return 支持的算法数量
     */
    public int getSupportedAlgorithmCount() {
        return algorithmCounts != null ? algorithmCounts.size() : 0;
    }
    
    /**
     * 获取已注册的类数量
     * 
     * @return 已注册的类数量
     */
    public int getRegisteredClassCount() {
        return registeredClassNames != null ? registeredClassNames.size() : 0;
    }
    
    /**
     * 检查是否有注册失败
     * 
     * @return 是否有注册失败
     */
    public boolean hasFailures() {
        return failedRegistrations > 0;
    }
    
    /**
     * 检查是否有成功注册
     * 
     * @return 是否有成功注册
     */
    public boolean hasSuccesses() {
        return successfulRegistrations > 0;
    }
    
    /**
     * 获取指定算法的注册数量
     * 
     * @param algorithm 加密算法
     * @return 注册数量
     */
    public int getAlgorithmCount(EncryptionAlgorithm algorithm) {
        if (algorithmCounts == null) {
            return 0;
        }
        return algorithmCounts.getOrDefault(algorithm, 0);
    }
    
    /**
     * 检查算法是否已注册
     * 
     * @param algorithm 加密算法
     * @return 是否已注册
     */
    public boolean isAlgorithmRegistered(EncryptionAlgorithm algorithm) {
        return getAlgorithmCount(algorithm) > 0;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RegistrationStatistics that = (RegistrationStatistics) o;
        return totalRegistrations == that.totalRegistrations &&
                successfulRegistrations == that.successfulRegistrations &&
                failedRegistrations == that.failedRegistrations &&
                Objects.equals(algorithmCounts, that.algorithmCounts) &&
                Objects.equals(registeredClassNames, that.registeredClassNames);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(totalRegistrations, successfulRegistrations, failedRegistrations, 
                           algorithmCounts, registeredClassNames);
    }
    
    @Override
    public String toString() {
        return "RegistrationStatistics{" +
                "totalRegistrations=" + totalRegistrations +
                ", successfulRegistrations=" + successfulRegistrations +
                ", failedRegistrations=" + failedRegistrations +
                ", successRate=" + String.format("%.2f%%", getSuccessRate() * 100) +
                ", supportedAlgorithms=" + getSupportedAlgorithmCount() +
                ", registeredClasses=" + getRegisteredClassCount() +
                ", timestamp=" + timestamp +
                '}';
    }
    
    /**
     * 获取详细统计信息
     * 
     * @return 详细统计信息字符串
     */
    public String getDetailedInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("Registration Statistics Summary:\n");
        sb.append("  Total Registrations: ").append(totalRegistrations).append("\n");
        sb.append("  Successful: ").append(successfulRegistrations).append("\n");
        sb.append("  Failed: ").append(failedRegistrations).append("\n");
        sb.append("  Success Rate: ").append(String.format("%.2f%%", getSuccessRate() * 100)).append("\n");
        sb.append("  Failure Rate: ").append(String.format("%.2f%%", getFailureRate() * 100)).append("\n");
        sb.append("  Supported Algorithms: ").append(getSupportedAlgorithmCount()).append("\n");
        sb.append("  Registered Classes: ").append(getRegisteredClassCount()).append("\n");
        
        if (algorithmCounts != null && !algorithmCounts.isEmpty()) {
            sb.append("  Algorithm Breakdown:\n");
            for (Map.Entry<EncryptionAlgorithm, Integer> entry : algorithmCounts.entrySet()) {
                sb.append("    ").append(entry.getKey()).append(": ").append(entry.getValue()).append(" classes\n");
            }
        }
        
        if (registeredClassNames != null && !registeredClassNames.isEmpty()) {
            sb.append("  Registered Classes:\n");
            for (String className : registeredClassNames) {
                sb.append("    ").append(className).append("\n");
            }
        }
        
        sb.append("  Generated at: ").append(timestamp).append("\n");
        
        return sb.toString();
    }
    
    /**
     * 获取简短统计信息
     * 
     * @return 简短统计信息字符串
     */
    public String getShortInfo() {
        return String.format("Registration: %d total, %d success (%.1f%%), %d failed, %d algorithms, %d classes",
                totalRegistrations,
                successfulRegistrations,
                getSuccessRate() * 100,
                failedRegistrations,
                getSupportedAlgorithmCount(),
                getRegisteredClassCount());
    }
}
